$(function () {
	//选择全部
	$('.checkall').click(function(){
		var ischecked = $(this).prop('checked');
	    $(this).parent().parent().nextAll('.main_in_tr_content').each(function(){
	    	$(this).find("input[type^='checkbox']").prop('checked',ischecked);
	    });
	});
	
//	$('#main_in_button_submit').click(function(){
//	    $('.main_in_button_div > .main_in_button_submit').hide();
//	    $('.main_in_button_div > .main_in_button_submit_s2').show();
//	});
	
	//关闭弹出层
	$('.fade_out_cancel').click(function(){
		fade_out_hide();
	});
	
	//模糊搜索鼠标选择
	$('body').on("mouseover mouseout",'.main_in_sbtn_fade_out_item',function(event){
		if(event.type == "mouseover"){
			$(this).addClass('main_in_sbtn_fade_out_item_cur');
			var tmp_html = $(this).html();
			$(this).parent().prevAll('input[type="text"]').val(tmp_html);
		}else if(event.type == "mouseout"){
			$(this).removeClass('main_in_sbtn_fade_out_item_cur');
		}
	});
	
	//取消模糊搜索框
	$('body').click(function(){
		$('.main_in_sbtn_fade_out').hide();
		$(document).unbind('keydown');
	});
	
	//模糊搜索鼠标点击赋值
	$('body').on('click','.main_in_sbtn_fade_out_item',function(){
		var tmp_html = $(this).html();
		$(this).parent().prevAll('input[type="text"]').val(tmp_html);
		$(this).parent('.main_in_sbtn_fade_out').hide();
	});
	
	//验证整型
	$('body').on('keyup','.checkint',function(){
		var tmp_int = parseInt($(this).val());
		tmp_int = isNaN(tmp_int) ? '' : tmp_int;
		$(this).val(tmp_int);
	});
	
	//验证浮点型，有floatlength属性
	$('body').on('keyup','.checkfloat',function(){
		var floatlength = $(this).attr('floatlength');
		var tmp_float = $(this).val();
		var tmp_test =  eval("/^\\d+(?:\\.{0,1})(\\d{1,"+floatlength+"})?$/");
		if(tmp_test.test(tmp_float)){
			tmp_float = tmp_float.replace(/^0+\./,'0.');
			tmp_float = tmp_float.replace(/^0+([0-9])/,'$1');
			$(this).val(tmp_float);
		}else{
			$(this).val(tmp_float.substring(0,tmp_float.length-1));
		}
		//$(this).val(Number($(this).val()));
	});
	
	//城市联动
	var select_city_top_2_cname_global = '';
	var select_city_top_3_cname_global = '';
	$(document).on('click','.select_city',function(){
		//select_city_top_2_cname_global = '';
		//select_city_top_3_cname_global = '';
		$('#show_select_city').remove();
		var select_city = $(this);
		var select_aid = 0;
		var tmp_showlv = 1;
		var select_city_id = $(this).parent().next('.select_city_id').val();
		
		var tmp_select_city_id = select_city_id.substring(4,6);
		if(Number(tmp_select_city_id) != 0){
			select_aid = select_city_id.substring(0,4)+'00';
			tmp_showlv = 3;
		}else{
			var tmp_select_city_id = select_city_id.substring(2,4);
			if(Number(tmp_select_city_id) != 0){
				select_aid = select_city_id.substring(0,2)+'0000';
				tmp_showlv = 2;
			}
		}
		if(tmp_showlv == 1){
			select_city_top_2_cname_global = '';
			select_city_top_3_cname_global = '';
		}
                
//                $.get('./?m=ajax&a=cityselect&aid='+select_aid,function(data){
                        var body = {
                             'TableName' : 'Area',
                            'PKName' : 'AID',
                            'where' : "Parent = '"+select_aid+"'",
                        }
                        $.ajax({
                            url: "http://api.unair.cn/api/PagingQuery",
                            type: "POST",
                            data: body,
                            success: function (data) {
                                var maxlv = select_city.attr('maxlv');
                                var select_city_top_2_aid = -1;
                                var select_city_top_2_cname = '';
                                var select_city_top_3_aid = -1;
                                var select_city_top_3_cname = '';
                                var item = '';
                                var item_end = '';
                                if(tmp_showlv == 3){
                                        select_city_top_3_aid = select_aid;
                                        select_city_top_3_cname = select_city_top_3_cname_global;
                                        select_city_top_2_aid = select_city_id.substring(0,2)+'0000';
                                        select_city_top_2_cname = select_city_top_2_cname_global;
                                        item_end = '<div class="select_city_top_all" cname="'+select_city_top_3_cname+'" aid="'+select_city_top_3_aid+'">全部</div>';
                                }else if(tmp_showlv == 2){
                                        select_city_top_2_aid = select_aid;
                                        select_city_top_2_cname = select_city_top_2_cname_global;
                                        //var item = '<div class="select_city_top_all" cname="'+select_city_top_2_cname+'" aid="'+select_city_top_2_aid+'">全部</div>';
                                }
                                var div_item = '<div id="show_select_city" showlv="'+tmp_showlv+'" maxlv="'+maxlv+'">'+
                                '<div class="select_city_top_1" aid="1">省</div>'+
                                '<div class="select_city_top_2" aid="'+select_city_top_2_aid+'" cname="'+select_city_top_2_cname+'">市</div>'+
                                '<div class="select_city_top_3" aid="'+select_city_top_3_aid+'" cname="'+select_city_top_3_cname+'">县</div>'+
                                '<div aid="-1"></div>'+
                                '</div>';
                                select_city.parent().parent().append(div_item);

                                data = eval("("+data['ResTable']+")");

                                $.each(data,function(k,v){
                                item += '<div cname="'+select_city_top_3_cname_global+v.Name+'" aid="'+v.AID+'">'+v.Name+'</div>';
                                });
                                item += item_end;
                                $('#show_select_city').append(item);
                                if(tmp_showlv == 1){
                                        var close_item = '<div class="select_city_top_close" aid="">关闭</div>';
                                        $('#show_select_city').append(close_item);
                                }
                            },
                            error: function (data) {
                                
                            }

                        });
			
//		});
		
//		$.get('./?m=ajax&a=cityselect&aid='+select_aid,function(data){
//			var maxlv = select_city.attr('maxlv');
//			var select_city_top_2_aid = -1;
//			var select_city_top_2_cname = '';
//			var select_city_top_3_aid = -1;
//			var select_city_top_3_cname = '';
//			var item = '';
//			var item_end = '';
//			if(tmp_showlv == 3){
//				select_city_top_3_aid = select_aid;
//				select_city_top_3_cname = select_city_top_3_cname_global;
//				select_city_top_2_aid = select_city_id.substring(0,2)+'0000';
//				select_city_top_2_cname = select_city_top_2_cname_global;
//				item_end = '<div class="select_city_top_all" cname="'+select_city_top_3_cname+'" aid="'+select_city_top_3_aid+'">全部</div>';
//			}else if(tmp_showlv == 2){
//				select_city_top_2_aid = select_aid;
//				select_city_top_2_cname = select_city_top_2_cname_global;
//				//var item = '<div class="select_city_top_all" cname="'+select_city_top_2_cname+'" aid="'+select_city_top_2_aid+'">全部</div>';
//			}
//			var div_item = '<div id="show_select_city" showlv="'+tmp_showlv+'" maxlv="'+maxlv+'">'+
//			'<div class="select_city_top_1" aid="1">省</div>'+
//			'<div class="select_city_top_2" aid="'+select_city_top_2_aid+'" cname="'+select_city_top_2_cname+'">市</div>'+
//			'<div class="select_city_top_3" aid="'+select_city_top_3_aid+'" cname="'+select_city_top_3_cname+'">县</div>'+
//			'<div aid="-1"></div>'+
//			'</div>';
//			select_city.parent().append(div_item);
//			
//			data = eval("("+data+")");
//                        alert(data)
//			$.each(data,function(k,v){
//	    		item += '<div cname="'+select_city_top_3_cname_global+v.Name+'" aid="'+v.AID+'">'+v.Name+'</div>';
//	    	});
//			item += item_end;
//			$('#show_select_city').append(item);
//			if(tmp_showlv == 1){
//				var close_item = '<div class="select_city_top_close" aid="">关闭</div>';
//				$('#show_select_city').append(close_item);
//			}
//		});
	});
	$(document).on('click','#show_select_city div',function(){
		var tmp_cname = $(this).attr('cname');
		var tmp_aid = $(this).attr('aid');
		var maxlv = $('#show_select_city').attr('maxlv');
		var showlv = 0;
		
		if(tmp_aid == -1){//空按钮
			
		}else{
			if($(this).hasClass('select_city_top_all')){//全部
                                $('#show_select_city').parent().find('.select_city_id').val(tmp_aid);
				$('#show_select_city').parent().find('.select_city').val(tmp_cname);
//				$('#show_select_city').prevAll('.select_city_id').val(tmp_aid);       投保app改
//				$('#show_select_city').prevAll('.select_city').val(tmp_cname);          投保app改
				$('#show_select_city').remove();
				return;
			}else if($(this).hasClass('select_city_top_close')){//关闭
				$('#show_select_city').remove();
				return;
			}else if($(this).hasClass('select_city_top_1')){//省
				tmp_aid = 0;
				tmp_cname = '';
				showlv = 0;
				select_city_top_2_cname_global = '';
				select_city_top_3_cname_global = '';
			}else if($(this).hasClass('select_city_top_2')){//市
				showlv = 1;
				select_city_top_2_cname_global = '';
				select_city_top_3_cname_global = '';
			}else if($(this).hasClass('select_city_top_3')){//县
				showlv = 2;
				select_city_top_3_cname_global = '';
			}else{
				showlv = $('#show_select_city').attr('showlv');
			}
			
			$('#show_select_city').prevAll('.select_city_id').val(tmp_aid);
			$('#show_select_city').prevAll('.select_city').val(tmp_cname);
			
			if(maxlv == showlv){
				$('#show_select_city').remove();
			}else{
				showlv++;
				$('#show_select_city').attr('showlv',showlv);
				$('#show_select_city').empty();
				
				var select_city_top_2_aid = -1;
				var select_city_top_2_cname = '';
				var select_city_top_3_aid = -1;
				var select_city_top_3_cname = '';
				if(showlv == 3){
					select_city_top_3_aid = tmp_aid;
					select_city_top_3_cname = tmp_cname;
					select_city_top_2_aid = tmp_aid.substring(0,2)+'0000';
					select_city_top_2_cname = select_city_top_2_cname_global;
					select_city_top_3_cname_global = tmp_cname;
				}else if(showlv == 2){
					select_city_top_2_aid = tmp_aid;
					select_city_top_2_cname = tmp_cname;
					select_city_top_2_cname_global = tmp_cname;
				}
				var div_item = '<div class="select_city_top_1" aid="1">省</div>'+
				'<div class="select_city_top_2" aid="'+select_city_top_2_aid+'" cname="'+select_city_top_2_cname+'">市</div>'+
				'<div class="select_city_top_3" aid="'+select_city_top_3_aid+'" cname="'+select_city_top_3_cname+'">县</div>'+
				'<div aid="-1"></div>';
				$('#show_select_city').append(div_item);
                                
//                                $.get('./?m=ajax&a=cityselect&aid='+tmp_aid,function(data){
                        var body = {
                             'TableName' : 'Area',
                            'PKName' : 'AID',
                            'where' : "Parent = '"+tmp_aid+"'",
                        }
                        $.ajax({
                            url: "http://api.unair.cn/api/PagingQuery",
                            type: "POST",
                            data: body,
                            success: function (data) {
                                data = eval("("+data['ResTable']+")");
					$.each(data,function(k,v){
			    		var item = '<div cname="'+tmp_cname+v.Name+'" aid="'+v.AID+'">'+v.Name+'</div>';
			    		$('#show_select_city').append(item);
                                        });
					if(showlv != 1){
						var item = '<div cname="'+tmp_cname+'" aid="'+tmp_aid+'" class="select_city_top_all">全部</div>';
						if(showlv == 2){
							var item = '';
						}
					}
		    		$('#show_select_city').append(item);
					if(showlv == 1){
						var close_item = '<div class="select_city_top_close" aid="">关闭</div>';
						$('#show_select_city').append(close_item);
					}
                            },
                            error: function (data) {
                                
                            }

                        });
					
//				});
				
//				$.get('./?m=ajax&a=cityselect&aid='+tmp_aid,function(data){
//					data = eval("("+data+")");
//					$.each(data,function(k,v){
//			    		var item = '<div cname="'+tmp_cname+v.Name+'" aid="'+v.AID+'">'+v.Name+'</div>';
//			    		$('#show_select_city').append(item);
//			    	});
//					if(showlv != 1){
//						var item = '<div cname="'+tmp_cname+'" aid="'+tmp_aid+'" class="select_city_top_all">全部</div>';
//						if(showlv == 2){
//							var item = '';
//						}
//					}
//		    		$('#show_select_city').append(item);
//					if(showlv == 1){
//						var close_item = '<div class="select_city_top_close" aid="">关闭</div>';
//						$('#show_select_city').append(close_item);
//					}
//				});
			}
		}
	});
	$(document).on('mouseover','#show_select_city div',function(){
		$('#show_select_city div').each(function(){
			if(!$(this).hasClass('select_city_top_1') && !$(this).hasClass('select_city_top_2') && !$(this).hasClass('select_city_top_3')){
				$(this).css('background-color','#FFF');
			}
		});
		if(!$(this).hasClass('select_city_top_1') && !$(this).hasClass('select_city_top_2') && !$(this).hasClass('select_city_top_3')){
			$(this).css('background','#02bcde');
		}
	});
	
	//弹出框关闭按钮
	$(document).on('click','#fade_out_alert_error .main_in_button_submit',function(){
		$('#fade_out_alert_error').prev('.outside_pull').remove();
		$('#fade_out_alert_error').remove();
		fade_out_hide();
	});
	$(document).on('click','#fade_out_alert_ok .main_in_button_submit',function(){
		var turl = $(this).attr('turl');
		$('#fade_out_alert_ok').prev('.outside_pull').remove();
		$('#fade_out_alert_ok').remove();
		fade_out_hide();
		if(typeof(turl) != 'undefined'){
			location.href = turl;
		}
	});
	
});

////删除单条
//function deltr(tableid){
//	$('#'+tableid).find('.main_in_tr_content').each(function(){
//        var ischecked = $(this).find("input[type^='checkbox']").prop('checked');
//        if(ischecked == true){
//            $(this).remove();
//	    }
//	});
//}
//
////删除全部
//function deltrall(tableid){
//	$('#'+tableid).find('.main_in_tr_content').remove();
//}
//
////显示弹出层，传id
function fade_out_pull(jqid){
	$('.outside_pull').show();
	$('#'+jqid).show();
	$('body').css('overflow','hidden');
}

////关闭弹出层
function fade_out_hide(){
	$('.outside_pull').hide();
	$('.fade_out').hide();
        $(".fade_out_Customized").hide();
	$('.fade_out_img').hide();
	$('body').css('overflow-x','hidden');
	$('body').css('overflow-y','auto');
}
//
////模糊搜索键盘选择，传方向-38上-40下,id
//function select_sbtn_fade_out_item(keycode,itemid){
//	var is_index = 0;
//	var show_index = 0;
//	if(keycode == "40"){
//		$('#'+itemid).next('.main_in_sbtn_fade_out').find('.main_in_sbtn_fade_out_item').each(function(){
//			is_index++;
//			if($(this).hasClass('main_in_sbtn_fade_out_item_cur')){
//				show_index = is_index;
//				$(this).removeClass('main_in_sbtn_fade_out_item_cur');
//			}
//		});
//		if ($('#'+itemid).next('.main_in_sbtn_fade_out').find('.main_in_sbtn_fade_out_item').eq(show_index).length == 0 ) { 
//			show_index = 0; 
//		}
//		$('#'+itemid).next('.main_in_sbtn_fade_out').find('.main_in_sbtn_fade_out_item').eq(show_index).addClass('main_in_sbtn_fade_out_item_cur');
//		var tmp_html = $('#'+itemid).next('.main_in_sbtn_fade_out').find('.main_in_sbtn_fade_out_item').eq(show_index).html();
//		//if(tmp_html != '' && typeof(tmp_html) != 'undefined'){
//			$('#'+itemid).val(tmp_html);
//			//console.log(show_index);//alert(show_index);
//		//}
//	}else if(keycode == "38"){
//		$('#'+itemid).next('.main_in_sbtn_fade_out').find('.main_in_sbtn_fade_out_item').each(function(){
//			is_index++;
//			if($(this).hasClass('main_in_sbtn_fade_out_item_cur')){
//				show_index = is_index;
//				$(this).removeClass('main_in_sbtn_fade_out_item_cur');
//			}
//		});
//		show_index = is_index - show_index - 1;
//		$('#'+itemid).next('.main_in_sbtn_fade_out').find('.main_in_sbtn_fade_out_item').eq(show_index).addClass('main_in_sbtn_fade_out_item_cur');
//		var tmp_html = $('#'+itemid).next('.main_in_sbtn_fade_out').find('.main_in_sbtn_fade_out_item').eq(show_index).html();
//		if(tmp_html != '' && typeof(tmp_html) != 'undefined'){
//			$('#'+itemid).val(tmp_html);
//		}
//	}
//	
//}
//
//function getlocation(address,returnid){
//	$.ajax({
//        url: 'http://api.map.baidu.com/geocoder/v2/?address='+address+'&output=json&ak=TRd4GWTeP3j8W6Hcfn04DEqx6o5XRldw',
//        type: "GET",
//        datatype: "json",
//        contentType: "application/json",
//        success: function (data) {
//        	
//        }
//    });
//}
//
//function gopaggind(url){
//    var tmp_page = $('.main_in_page_input').val();
//    location.href = url+tmp_page;
//}
//
function alert_error(msg){
	var html = '<div class="outside_pull"></div>'+
	'<div class="fade_out_alert" id="fade_out_alert_error">'+
	'<img class="fade_out_alert_img" src="./img/alert_error.png" />'+
	'<div class="fade_out_alert_text">'+msg+'</div>'+
        '<div style="clear:both"></div>'+
	'<div class="main_in_button_submit">确定</div>'+
	'</div>';
	$('body').append(html);
	$('.outside_pull').show();
	$('#fade_out_alert_error').show();
	$('body').css('overflow','hidden');
}

function alert_ok(msg,url){
	var lu = '';
	if(url != ''){
		lu = 'turl="'+url+'"';
	}
	var html = '<div class="outside_pull"></div>'+
	'<div class="fade_out_alert" id="fade_out_alert_ok">'+
	'<img class="fade_out_alert_img" src="./img/alert_ok.png" />'+
	'<div class="fade_out_alert_text">'+msg+'</div>'+
	'<div class="main_in_button_submit" '+lu+'>确 定</div>'+
	'</div>';
	$('body').append(html);
	$('.outside_pull').show();
	$('#fade_out_alert_ok').show();
	$('body').css('overflow','hidden');
}

function getUrlParam(name) {
 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
 var r = window.location.search.substr(1).match(reg); //匹配目标参数
 if (r != null) return unescape(r[2]); return null; //返回参数值
}
//
//
//
